<?php


namespace app\admin\controller;


use app\admin\controller\BaseController;
use app\common\model\mysql\DriverFile as DriverFileModel;
use app\common\exception\MessageException;
use app\common\validate\IDMustBePositiveInt;
use app\common\validate\NameValidate;


class DriveFile extends BaseController
{
    public function list()
    {
        try {
            $params = $this->request->param();

            if (!isset($params['folder_id']) || $params['folder_id'] == '') {
                $params['folder_id'] = 0;
            }

            $result = DriverFileModel::list($params);
            return $this->renderSuccess($result);
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    /**
     * 文件重命名
     * @return \think\response\Json|void
     * @throws MessageException
     * @throws \app\common\exception\ParameterException
     */
    public function update()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不合法']);
        }

        (new IDMustBePositiveInt())->goCheck();
        (new NameValidate())->goCheck();
        $params = $this->request->param();

        try {
            $Model = new DriverFileModel();
            $findData = $Model->find($params['id']);

            if (!$findData) {
                return $this->renderFail('分类不已存在');
            }

            $data = $Model->update($params);

            if ($data) {
                return $this->renderSuccess([], '更新成功');
            }

        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    /**
     * 删除文件
     * @return \think\response\Json|void
     * @throws MessageException
     */
    public function batchDelete()
    {
        $delIds = $this->request->param('ids');
        if (!is_array($delIds)) {
            throw new MessageException(['msg' => '参数不合法']);
        }
        try {
            $Model = new DriverFileModel();
            foreach ($delIds as $id) {
                // 保存数据库记录
                $result[$id] = $Model->update(['id' => $id, 'status' => config('status.mysql.delete')]);
            }
            return $this->renderSuccess($result[$id], '删除成功');
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    /**
     * 删除文件
     * @return \think\response\Json|void
     * @throws MessageException
     */
    public function batchMove()
    {
        $delIds = $this->request->param('ids');
        $folderId = $this->request->param('folder_id');

        if (!is_array($delIds) || empty($folderId)) {
            throw new MessageException(['msg' => '参数不合法']);
        }

        try {
            $Model = new DriverFileModel();
            $result = array();
            foreach ($delIds as $id) {
                // 保存数据库记录
                $result[$id] = $Model->update(['id' => $id, 'folder_id' => $folderId]);
            }
            return $this->renderSuccess($result, '移动成功');
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }
}